package com.bba.fms.common.ezspecial.util;

import com.bba.fms.common.ezspecial.core.EzSpecial;
import com.bba.fms.common.ezspecial.dto.FieldDto;

import java.awt.image.RasterFormatException;
import java.lang.reflect.Field;

/**
 * @BelongsProject: fms
 * @Author: 王志鹏
 * @CreateTime: 2019-10-14 13:56
 * @description: TODO
 */
public class ReflexUtil {
    private ReflexUtil(){}

    /**
     *获得注解
     * @param cls
     * @param v
     * @return
     */
    public static FieldDto getFiledName(Class cls, String v) {
        FieldDto fileDto = null;

        Field[] fields = cls.getDeclaredFields();
        for (Field f : fields) {
            f.setAccessible(true);

            if (f.isAnnotationPresent(EzSpecial.class)) {

                EzSpecial ezSpecial = f.getAnnotation(EzSpecial.class);
                if ((ezSpecial.value()).equals(v)) {
                    fileDto = new FieldDto();
                    String[] check = ezSpecial.check();
                    fileDto.setCheck(check);
                    fileDto.setColoumName(f.getName());
                    fileDto.setRegular(ezSpecial.reglar().getRule());
                    fileDto.setRequire(ezSpecial.require());
                }
            }
        }

        if (fileDto == null) throw new RasterFormatException("未找到该类属性!");
        return fileDto;
    }
}
